﻿using MES.Desktop.Common;
using MES.Desktop.IService;
using MES.Desktop.SystemModule.Models;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MES.Desktop.SystemModule.ViewModels.Dialogs
{
    public class ModifyDepartViewModel : DialogViewModelBase
    {
        public DepartModel DepartInfo { get; set; } =
                new DepartModel();


        IDepartService _departService;
        public ModifyDepartViewModel(IDepartService departService)
        {
            _departService = departService;
            DepartInfo = new DepartModel(departService);
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            var model = parameters.GetValue<DepartModel>("model");

            if (model == null)
            {
                Title = "新增部门信息";
            }
            else
            {
                Title = "编辑部门信息";

                DepartInfo.DepartId = model.DepartId;
                DepartInfo.DepartName = model.DepartName;
            }
        }

        public override void DoSave()
        {
            if (DepartInfo.HasErrors) return;
            try
            {
                Entities.DepartEntity departEntity = new Entities.DepartEntity
                {
                    DepartId = DepartInfo.DepartId,
                    DepartName = DepartInfo.DepartName
                };

                _departService.UpdateDepart(departEntity);

                base.DoSave();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
